
// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import store from './store'
Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import App from './App.vue'
import store from './store'
export function createApp() {
  const app = createSSRApp(App)
  app.use(store)
  
  
  
  console.log(app.$store,"app.$store---",store)

  
	//公共JS
  let appStore = store;
  let cTop = uni.getSystemInfoSync().statusBarHeight + 44;
  let SystemInfoSync = {cTop};
  let menuButtonInfo = {};
  // #ifndef H5
  menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  SystemInfoSync = {...SystemInfoSync,...menuButtonInfo}
  // #endif
  // #ifdef H5
  SystemInfoSync = {
	  bottom: 56,
	  cTop: 44,
	  height: 32,
	  left: 226,
	  right: 313,
	  top: 24,
	  width: 87,
  }
  // #endif
  
  
  const appMsg = (title, duration = 1500, mask = false, icon = 'none') => {
  	//统一提示方便全局修改
  	title = title || "请输入完整信息";
  	uni.showToast({
  		title,
  		duration,
  		mask,
  		icon
  	});
  }
  app.config.globalProperties.$msg = appMsg;
  
  appStore.commit('setJiaonang',SystemInfoSync);
  let apiUrl = uni.getStorageSync("baseUrl") || appStore.state.baseUrl;
  let apiPath = appStore.state.apiPath;
  const queryApi = function(method,url, data, callsuc, token,noMsg,err) {
  	method = method || "post";
  	token = token == "false" ? '' : uni.getStorageSync('token');
	
  	data = data ? data : {};
  	let userInfo = appStore.state.userInfo; 
  	uni.showLoading();
  	let userInfoId = ""
  	 try{
  	 	userInfoId = appStore.state.userInfo || {};
  	 }catch(e){
  	 	//TODO handle the exception
  	 }
	 
  	// if (token) {
  	// 	header.token = "1aa76bf5-a1e8-4188-ba1c-b9009b3bbb0e";
  	// }
  	uni.request({
  		url: apiUrl + apiPath + url,
  		method: 'post',
  		data: data,
  		header: {
			Accept: 'text/json',
			'Content-Type': 'application/json;charset=UTF-8',
  			// 'content-type': 'application/x-www-form-urlencoded'
			platform:'WechatMiniProgram',
			token 
  		},
  		success: (res) => {
  			uni.hideLoading();
  			console.log(res,"res--93")
  			err instanceof Function && err(res.data);
  			if (res.data.code == 1) {
  				callsuc instanceof Function && callsuc(res.data);
  				if(res.data.msg && noMsg != "noMsg"){
  					appMsg(res.data.msg)
  				}
  			} else
  			if (res.data.code == 0) {
  				try{
  					uni.hideLoading();
  				}catch(e){
  					//TODO handle the exception
  				}
  				if(res.data.msg.indexOf("已领取过该优惠券") != -1){
  					uni.setStorageSync('lingquQ',1)
  				}
  				if(res.data.msg.indexOf("token is error") !=  -1 ){
  					uni.reLaunch({
  						url:"/pages/login/login"
  					})
  				}else{
  					appMsg(res.data.msg)
  				}
  				return false
  			} else
  			if (res.data.code == 401) {
				appStore.commit("logout");
  				appMsg("请重新登录")
  				// // #ifndef MP
  				// 	appStore.commit("logout");
  				// 	uni.switchTab({
  				// 		url: '/pages/index/index'
  				// 	})
  				// // #endif
  				// // #ifdef MP
  				// 	appStore.commit("logout");
  				// 	setTimeout(()=>{
  				// 		uni.switchTab({
  				// 			url: '/pages/index/index'
  				// 		})
  				// 	},1000)
  				// // #endif
  			} 
  		},
  		fail: (err) => {
  			appMsg("网络请求出错")
  			
  			setTimeout(()=>{
  				uni.hideLoading();
  				uni.reLaunch({
  					url:"/pages/login/noData"
  				})
  			},4000)
  			
  			
  		}
  	});
  }
  app.config.globalProperties.$queryApi = queryApi;
  // 获取用户信息
  app.config.globalProperties.$resetUserInfo = function(back) {
	queryApi("post","/user.user/profile", {}, res => {	
		appStore.commit("login",res.data)
		back instanceof Function  && back()
	},"","noMsg")
  }
  app.config.globalProperties.$getApiTime = function(url, data, callsuc, token) {
  	token = token == "false" ? false : true;
  	data = data ? data : {};
  	console.log("token", token);
  	let userInfo = uni.getStorageSync('userInfo');
  	if (token && userInfo) {
  		data.token = userInfo.remember_token;
  		data.page = 1;
  		data.paginate = 200;
  	}
  	uni.request({
  		url: apiUrl + url,
  		method: 'post',
  		data: data,
  		header: {
  			'content-type': 'application/x-www-form-urlencoded'
  		},
  		success: (res) => {
  			console.log(res)
  			if (res.data.status == 200 || res.data.status == 202) {
  				// appMsg(res.data.msg)
  				callsuc instanceof Function && callsuc(res.data)
  			} else
  			if (res.data.status == 401) {
  				appMsg("登录已过期,请重新登录")
  				// #ifndef MP
  					appStore.commit("logout");
  					uni.reLaunch({
  						url: '../login/yLogin',
  						success(){
  							// location.reload()
  						}
  					})
  				// #endif
  				// #ifdef MP
  					appStore.commit("logout");
  					callsuc instanceof Function && callsuc(res.data)
  					
  				// #endif
  			}
  		},
  		fail: (err) => {
  			appMsg("网络错误,请联系管理员")
  		}
  	});
  }
  

  
  app.config.globalProperties.$getDate = (myDate,type,timec) => {
  	if(!myDate){
  		myDate = new Date()
  	}else{
  		if(timec == "c"){
  			myDate = new Date(parseInt(myDate) * 1000)
  		}else{
  			myDate = new Date(myDate)
  		}
  	}
  	let year = myDate.getFullYear();   //获取系统的年；
  	let month = myDate.getMonth()+1;   //获取系统月份，由于月份是从0开始计算，所以要加1
  	
  	let date = myDate.getDate(); // 获取系统日，
  	
  	let hours = myDate.getHours(); //获取系统时，
  	let min = myDate.getMinutes(); //分
  	let sec = myDate.getSeconds(); //秒
  	if(month<10){ month = "0"+month}
  	if(date<10){ date = "0"+date }
  	if(hours<10){ hours = "0"+hours}
  	if(min<10){ min = "0"+min}
  	if(sec<10){ sec = "0"+sec }
  	
  	if(type == '月-号'){
  		return month+'月'+date+'号'
  	}else
  	if(type == '年-月-日'){
  		return year+'年'+month+'月'+date+'日'
  	}else
  	if(type == 's-s-s'){
  		return year+'-'+month+'-'+date
  	}else
  	if(type == 's:s:s'){
  		return hours+':'+min+':'+sec
  	}else
  	if(type == 's-s-s s:s:s'){
  		return year+'-'+month+'-'+date+" "+hours+':'+min+':'+sec
  	}else{
  		return year+'-'+month+'-'+date+" "+hours+':'+min+':'+sec
  	}
  }
  
  app.config.globalProperties.$calcPhone = function(el){
  	if(el){
  		return el.substr(0,3) + "****" + el.substr(7)
  	}else{
  		return "未绑定手机号"
  	}
  }
  
  app.config.globalProperties.$qianweifu = function(num){
  	num=num.toString().split(".");  // 分隔小数点
  	    var arr=num[0].split("").reverse();  // 转换成字符数组并且倒序排列
  	    var res=[];
  	    for(var i=0,len=arr.length;i<len;i++){
  	      if(i%3===0&&i!==0){
  	         res.push(",");   // 添加分隔符
  	      }
  	      res.push(arr[i]);
  	    }
  	    res.reverse(); // 再次倒序成为正确的顺序
  	    if(num[1]){  // 如果有小数的话添加小数部分
  	      res=res.join("").concat("."+num[1]);
  	    }else{
  	      res=res.join("");
  	    }
  	    return res;
  }
  //公共JS end
  
  return {
    app
  }
}

// #endif